<template>
	<view :class="[primarySize()]">
		<view class="page-foot" v-if="userinfo.group_id==2">
			<Footer :identity="2" :footState="1" :num="megNum"></Footer>
		</view>
		<view class="page-foot" v-else>
			<Footer :identity="1" :footState="1" :num="megNum"></Footer>
		</view>
		<view class="container bg-white">
			<view class="page-body">
				<view class="communicate">
					<view class="item flex-box m-hairline--bottom" v-for="(item,index) in sessionList" @click="bindChat"
						:data-index="index">
						<image class="img_head"
							:src="cdnurl(userinfo.id!=item.receive_user_id?item.receiveuser.avatar:item.senduser.avatar)"
							mode="aspectFill"></image>
						<view class="flex-grow-1">
							<view class="flex-box row1">
								<view class="name" :class="[item.unread_num>0?'dot':'']">
									{{userinfo.id!=item.receive_user_id?item.receiveuser.nickname:item.senduser.nickname }}
								</view>
								<view class="date">{{item.newmsg.createtime_text}}</view>
							</view>
							<view class="message" v-if="item.newmsg.type==6">【面试邀请】</view>
							<view class="message" v-if="item.newmsg.type==5">【人才】</view>
							<view class="message" v-if="item.newmsg.type==4">【投递咨询】</view>
							<view class="message" v-else-if="item.newmsg.type==3">【职位咨询】</view>
							<view class="message" v-else-if="item.newmsg.type==2">【图片】</view>
							<view class="message" v-else-if="item.newmsg.type==1">{{item.newmsg.message}}</view>
						</view>
					</view>

			<view class="nothing" v-if="sessionList.length<=0">
				<image class="img_nothing" src="/static/public/icon_nothing.png" mode="aspectFill"></image>
				<text>暂无沟通</text>
			</view>
			
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {

				userinfo: null,
				megNum: 0,
				personal: {
					total: 0,
					message: null
				},
				activity: {
					total: 0,
					message: null
				},
				system: {
					total: 0,
					message: null
				},
				sessionList: [],
				sessionListMore: {
					page: 1,
					pagesize: 50,
					text: '加载中...'
				},
				megNum: 0,
			}
		},
		onReady() {

			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.primaryColor()
			})

		},
		onLoad: function(options) {

			let page = this;
			
				this.userinfo = this.$core.getUserinfo(true);
			// this.refresh();
			//获取列表成功回调
			uni.$on("session_list_callback", function(data) {
				let curPageData = data.data;
				page.sessionList = curPageData;
				page.sessionListMore = {
					page: 1,
					pagesize: 50,
					text: ''
				};
			})
			uni.$on(app.globalData.Events.UserLogin, this.eventOnUserLogin, this);
			uni.$on(app.globalData.Events.UserLogout, this.eventOnUserLogout, this);
			uni.$on(app.globalData.Events.UserinfoChange, this.eventOnUserinfoChange, this);
			
			uni.$on(app.globalData.Events.WebsocketMessageCount, this.eventWebsocketMessageCount, this);
			//刷新列表
			uni.$on('refresh_session', function() {
				// if (app.globalData.chat_config.SocketTask) {
					page.$chatIM.setMessageBadge(false)
				// }
				page.refresh();
			})
		},
		onShow() {
			let page = this;
			//未读消息
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: this.primaryColor()
			})
			this.refresh();
		},
		onUnload: function() {
			uni.$off(app.globalData.Events.UserLogin, this.eventOnUserLogin);
			uni.$off(app.globalData.Events.UserLogout, this.eventOnUserLogout);
			uni.$off(app.globalData.Events.UserinfoUpdate, this.eventOnUserinfoUpdate);
			uni.$off(app.globalData.Events.WebsocketMessageCount, this.eventWebsocketMessageCount);
			uni.$off('session_list_callback');
			uni.$off('refresh_session');
		},
		onPullDownRefresh() {
			this.refresh();
		},
		methods: {

			//刷新
			refresh() {
				this.sessionListMore = {
					page: 1,
					pagesize: 50,
					text: '加载中...'
				}
				this.sessionLists();
				uni.stopPullDownRefresh();
				this.$forceUpdate()
			},
			bindGoPersonal() {
				let total = this.personal.total;
				if (total > 0) {
					this.personal.total = 0;
					uni.$emit(app.globalData.Events.ClearMessageCount, total);
				}
				uni.navigateTo({
					url: '/pages/public/notice_list?type=personal'
				});
			},
			bindGoActivity() {
				let total = this.activity.total;
				if (total > 0) {
					this.activity.total = 0;
					uni.$emit(app.globalData.Events.ClearMessageCount, total);
				}
				uni.navigateTo({
					url: '/pages/public/notice_list?type=activity'
				});
			},
			bindGoSystem() {
				let total = this.system.total;
				if (total > 0) {
					this.system.total = 0;
					uni.$emit(app.globalData.Events.ClearMessageCount, total);
				}
				uni.navigateTo({
					url: '/pages/public/notice_list?type=system'
				});
			},
			//聊天列表
			sessionLists() {
				let sessionListMore = this.sessionListMore;
				this.$chatIM.wsSend({
					type: 'sessionlist',
					page: sessionListMore.page,
					pagesize: sessionListMore.pagesize
				});
				uni.stopPullDownRefresh();
			},
			//前往聊天室
			bindChat(event) {
				let index = event.currentTarget.dataset.index;
				let item = this.sessionList[index];
				let receive_user_id = this.userinfo.id != item.receive_user_id ? item.receiveuser.id : item.senduser.id;
				let to_username = this.userinfo.id != item.receive_user_id ? item.receiveuser.nickname : item.senduser
					.nickname;
				let chat = {
					company_id: item.company_id,
					resume_id: item.resume_id,
					receive_user_id: receive_user_id,
					to_username: to_username
				}
				chat.refresh_status = item.unread_num > 0 ? 1 : 0;
				uni.navigateTo({
					url: '/pages/public/chat?param=' + JSON.stringify(chat),
					events: {
						refreshSession: data => {
							this.$chatIM.setMessageBadge(false)
						}
					},
					// success(res) {
					// 	res.eventChannel.emit('chatTransfor', chat)
					// }
				})
			},

			eventOnUserLogin(userinfo) {
				this.userinfo = this.$core.getUserinfo()
				this.refresh();
			},
			eventOnUserLogout() {
				this.userinfo = []
				this.refresh();
			},
			eventOnUserinfoChange: function() {
				this.refresh();
			},
			//未读消息
			eventWebsocketMessageCount(data) {
				this.megNum = this.userinfo ? app.globalData.chat_config.messageCount : 0;
			// this.$forceUpdate()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page-body {
		padding: 10rpx 30rpx;
	}

	.communicate .item {
		padding: 30rpx 0;
	}

	.communicate .img_head {
		margin: 0 20rpx 0 0;
		display: block;
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
	}

	.communicate .row1 {
		margin: 0 0 20rpx;
		justify-content: space-between;
	}

	.communicate .name {
		color: #101010;
		font-size: 30rpx;
		line-height: 36rpx;
	}

	.communicate .name.dot {
		position: relative;
	}

	.communicate .name.dot::after {
		content: "";
		position: absolute;
		top: 2rpx;
		right: -20rpx;
		width: 14rpx;
		height: 14rpx;
		background: #ff5332;
		border-radius: 50%;
	}

	.communicate .date {
		color: #aaaaaa;
		font-size: 24rpx;
		line-height: 29rpx;
	}

	.communicate .message {
		color: #555555;
		font-size: 30rpx;
		line-height: 36rpx;
	}

	.communicate .flex-grow-1 {
		overflow: visible;
	}
</style>